import 'package:built_collection/built_collection.dart';
import 'package:equatable/equatable.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/app_navigator_transition_info.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/page_models/app_navigator_page_model.dart';
import 'package:learning_content_admin_pannel.core/core/domain/entities/page_models/settings_page_model.dart';
import 'package:learning_content_admin_pannel.core/core/domain/helpers/handle_root_navigator_page_push_request/use_case.dart';

part 'result.dart';

abstract class HandleSettingsPageOpeningRequest {
  SettingsPageOpeningRequestHandlingResult call({
    required BuiltList<AppNavigatorPageModel> rootNavigatorPageModels,
    required BuiltMap<AppNavigatorPageModel, AppNavigatorTransitionInfo>
        rootNavigatorPageModelToTransitionInfo,
  });
}

class HandleSettingsPageOpeningRequestImpl implements HandleSettingsPageOpeningRequest {
  const HandleSettingsPageOpeningRequestImpl({
    required HandleRootNavigatorPagePushRequest handleRootNavigatorPagePushRequest,
  }) : _handleRootNavigatorPagePushRequest = handleRootNavigatorPagePushRequest;

  final HandleRootNavigatorPagePushRequest _handleRootNavigatorPagePushRequest;

  @override
  SettingsPageOpeningRequestHandlingResult call({
    required BuiltList<AppNavigatorPageModel> rootNavigatorPageModels,
    required BuiltMap<AppNavigatorPageModel, AppNavigatorTransitionInfo>
        rootNavigatorPageModelToTransitionInfo,
  }) {
    if (rootNavigatorPageModels.last is SettingsPageModel) {
      final result = SettingsPageOpeningRequestHandlingResult(
        rootNavigatorPageModels: rootNavigatorPageModels,
        rootNavigatorPageModelToTransitionInfo: rootNavigatorPageModelToTransitionInfo,
      );

      return result;
    }

    final settingsPageModel = SettingsPageModel();

    final rootNavigatorPagePushHandlingResult = _handleRootNavigatorPagePushRequest(
      pageModel: settingsPageModel,
      rootNavigatorPageModels: rootNavigatorPageModels,
      rootNavigatorPageModelToTransitionInfo: rootNavigatorPageModelToTransitionInfo,
    );

    final result = SettingsPageOpeningRequestHandlingResult(
      rootNavigatorPageModels: rootNavigatorPagePushHandlingResult.rootNavigatorPageModels,
      rootNavigatorPageModelToTransitionInfo:
          rootNavigatorPagePushHandlingResult.rootNavigatorPageModelToTransitionInfo,
    );

    return result;
  }
}
